package Cluedo.AI;

import Cluedo.API.*;
import Cluedo.Game.*;
import Cluedo.Controller.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.StringBuffer;

public class CustomAI2 implements 
	Runnable, Cluedo.API.AIContract {

	static int sleepPeriod = 200;

	String myName = "";
	
	boolean dead = false;
	
	boolean hasMadeFalseAccusation = false;
	
	Vector inbox = new Vector();
	
	String motive = "";

	Position destination = null;

	boolean disproofReceived = true;
	
	Message incomingDisproof = null;
	
	String [] motives = { "findRoom", "walkToRoom",};
	
	private String screenOutput = "";
	
	boolean hasRolledDice = false;
	
	boolean hasSuggested = false;
	
	boolean hasListened = false;
	
	boolean hasDisproved = false;
	
	boolean hasUsedGoToRoom = false;
	
	String disprover;
	
	String suggester;
	
	int allowed_moves = 0;
	
	boolean disproved = false;
	
	int i = 20;

	CluedoListener cluedoListener = new CluedoListener() {
		
		public void localMessage(Cluedo.API.Message message) {}
	
		
		public void notifyAlert(Message message) {
			String output = "";			
		
			// One case where we need a little help..
			if(disproofReceived == false) {
				if(message.type.equals("disproofAlert")) {
					disproofReceived = true;
					incomingDisproof = message;
					inbox.add(message);
					
				}

			}
			else
				inbox.add(message);
			
		}
        	
        	

		public CluedoListener getCluedoListener() {
			return this;
		}
	
	};

	public CluedoListener getCluedoListener () {
		return cluedoListener;
	}


	private void sleepFor(int interval) {
		try {
			Thread.sleep(interval);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public CustomAI2 (String args, GameController gameController, String AIName) {
		//super(args);
		myName = AIName;
		this.gameController = gameController;
		System.out.println("[CustomAI2:] NAME = " + myName);
		bindAI();
		motive = "findRoom";
	}


	public void bindAI() {
		try {
			gameController.bindListener(getCluedoListener());
		}
		catch(Exception Error) {
			System.out.println("[CustomAI13:BindAI" + myName + "]: can't bind");
		}
	}
	
	synchronized public void die() {
		dead = true;
	}
	
	public void run () {
		while( dead == false ) {
			try {

				try {
					Thread.sleep(100);
				}
				catch (InterruptedException e) {
					e.printStackTrace();
				}

				if(inbox.size() > 0) {
					while(inbox.size() > 0) {
						Thread.sleep(10);
						handleMessage(((Message)inbox.firstElement()));	
						inbox.remove(inbox.firstElement());
					}
				}
			}
			catch (InterruptedException e) {
				
			}
		}
	}
	
	 public void handleMessage(Message message) {
		String output = "";

		if(gameController.getBoard().getGameFinished() == false) {
			//if(message.type.equals("commenceGame"))
				//message.type = "nextPlayer";
				
			if(message.type.equals("newRoll")) {
                	
			}
			if(message.type.equals("commenceGame")) {
				message.type = "nextPlayer";
				sleepFor(4000);
			}
			else if(message.type.equals("newEnvelope")) {
                	
			}
			else if(message.type.equals("suggestionAlert")) {
				if(message.data.equals(myName)) {
					
					handleSuggestionAlert(message);
				}
				
			}
			else if(message.type.equals("disproofAlert")) {
					outputMessage("\n[CustomAI2 " + myName + "]: Heard a disproofAlert ...\n");
					if(disproofReceived == false) {
						DisproofAlert d = ((DisproofAlert)message.parameters.get(0));
						deductFromDisproof(d);
					}
					disproofReceived = true;
			}
			else if(message.type.equals("nextPlayer")) {
				if(gameController.getBoard().getCurrentPlayer().getRealName().equals(myName)) {
					if (hasMadeFalseAccusation == false){
						takeTurn();
					}

                	
				}
			}
			else if(message.type.equals("moveAlert")) {
                	
			}		
		}
		else {
			if(dead == false) {
				outputMessage("[CustomAI2 " + myName + "]: Game is over and i'm quitting..");
				die();
			}
		}
		if(!output.equals(""))
			outputMessage(output);
	}



	private boolean isEliminated(String name) {
		Player me = gameController.getBoard().getCurrentPlayer();
		
		return me.getDetectivePad().getCardList().cardByName(name).isEliminated();
	}


	public void setMyName(String myName) {
		this.myName = myName;
	}


	public void initaliseAI() {
		System.out.println("[CustomAI2 " + myName + "]: Initalised "+myName+".");
	}


	GameController gameController = null;

	Cluedo.GUI.TextParser textParser;

	Cluedo.GUI.TextEventHandler textHandler = new Cluedo.GUI.TextEventHandler(this);

	CluedoConfig gameConfig = null;
	int debugLevel = 0;
	

	synchronized void outputMessage(String output ) {
		StringTokenizer lines = new StringTokenizer(output, "\n");
		while ( lines.hasMoreElements() == true ) {
			System.out.println("[AI:"+myName+"] " + lines.nextToken() );
			screenOutput += "[AI:"+myName+"] " + lines.nextToken();
		}
		System.out.println();
	}

	

	synchronized public String endGo() {
		String output = "";

		gameController.directCommand("nextPlayer", null);
			
		return output;
	}


	synchronized public String buildCards() {
		StringBuffer buffer = new StringBuffer();
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myCards = me.getCards();
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		for(int i = 0; i < myDCards.countCards(); i++) {
			buffer.append("- " + myDCards.cardAt(i).getName() +"\t" + myDCards.cardAt(i).isEliminated()+"\n");
		}
		
		
		return buffer.toString();
	}

	synchronized public void takeTurn() {
		String output = "";
		outputMessage("[CustomAI2 " + myName + "]: takeTurn..");

		i--;

		sleepFor(sleepPeriod);

			

		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myCards = me.getCards();
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
			
		sleepFor(sleepPeriod);
			

		runDecideOnMovementEvent();
		
		sleepFor(sleepPeriod);
		
		runSuggestOrAccuseEvent();
	
		hasRolledDice=false;
		hasSuggested=false;
		hasListened=false;
		hasDisproved=false;
		hasUsedGoToRoom=false;
	}
		
		
	Position findNeighbour(Position node) {
		Position found = null;
		
		for(int i = 0; i < node.getNeighbours().size(); i++)
			if(
				(node.getNeighbours().elementAt(i)) instanceof Room == true
				||
				((Position)node.getNeighbours().elementAt(i)).isOccupied() == false) {

				found = (Position)node.getNeighbours().elementAt(i);
			}

		return found;
		
	}
	
	synchronized public void walkToRoom (Position destination) {
		String walkInfo = "";
		String output = "";
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myCards = me.getDetectivePad().getCardList();
		
		allowed_moves = rollDice();
		

		
		walkInfo += "Let's see, what the best path to the "+destination.getID()+" is..\n";
		
		long start = (new java.util.Date()).getTime();
		
		Vector v = gameController.getBoard().map.routeTo(me.getPosition(), destination);
		
		long stop = (new java.util.Date()).getTime();
		
		outputMessage("Finding shortest path: "+me.getPosition().getID()+" to "+destination.getID()+" ("+(stop-start)+"ms)");
		
		if(v.size()-1 == -1) {
			destination = null;
			Vector rooms = gameController.getBoard().map.getNodes("rooms");

			for(int i = 0; i < rooms.size(); i++) {
				v = gameController.getBoard().map.routeTo(me.getPosition(), (Position) rooms.elementAt(i));
			
				if(v.size()-1 == -1) {
					continue;
				}
				else {
					destination = (Position) rooms.elementAt(i);
					break;
				}


			}
			if(destination == null)
				return;

		}

		walkInfo += "Hmm.. about "+(v.size()-1)+" moves, i'd say..\n";
		walkInfo += "Starting at: ";
		outputMessage(walkInfo);
		
		for(int i = 0; i < v.size(); i++)
			walkInfo += " -> " + ((Position)v.elementAt(i)).getID();
		
		walkInfo += "\nWell, I'm allowed to make " + allowed_moves+" moves, let's see about that\n";

		if (v.size() > allowed_moves+1 ) {
			//output += "\nSlowly slowly - catchy monkey\n";
			outputMessage("\n[CustomAI2 " + myName + "]: About to move\n");
			Room stopOver = null;
			int i = 0;
			for(i = 1; i < allowed_moves+1; i++)
				if( v.elementAt(i) instanceof Room) {
					stopOver = ((Room)v.elementAt(i));
					break;
				}
				
			if(stopOver == null) {
								
				movePlayer(((Position)v.elementAt(allowed_moves)), v, v.size());
				//movePlayer(((Position)v.elementAt(allowed_moves)), v.size());

			}
			else {
				output += "- Room in my path, must stop here: " +stopOver.getID() + "\n";
				movePlayer(stopOver, v, i+1);

			}
		}
		else {
			output += "\nEnough moves remaining to get to target.\n";
			//end go
			Room stopOver = null;
			int i = 0;
			for(i = 1; i < v.size(); i++)
				if( v.elementAt(i) instanceof Room) {
					stopOver = ((Room)v.elementAt(i));
					break;
				}
				
			if(stopOver == null) {
				movePlayer(destination, v, v.size());

			}
			else {
				output += "- Room in my path, must stop here: " +stopOver.getID() + "\n";
				movePlayer(stopOver, v, i+1);

			}
			motive = "findRoom";
		}
		if(me.getPosition() == destination ) {
			motive = "findRoom";
			output += "I got to my destination!!\n";
		}
        	
		sleepFor(sleepPeriod);
		if(me.getPosition() instanceof Room) {
			output += "Erm.. I could make a quick suggestion here probably.\n";
        	
			outputMessage("[CustomAI2 " + myName + "]: " + output);
		}
	
	}

	synchronized public Position findRoom() {
		return findRoom(false);
	}
	
	synchronized public Position findRoom(boolean randomFirst) {
		Position destination = null;
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		Vector rooms = gameController.getBoard().map.getNodes("rooms");
		
		if(randomFirst == false) {
			for(int i = 0; i < rooms.size(); i++)
				if(myDCards.cardByName(((Room)rooms.elementAt(i)).getID()).isEliminated() == false) {
					destination = ((Room)rooms.elementAt(i));
				}
		}

		if(destination == null || randomFirst == true) {
			int index = (int) (Math.random() * (rooms.size()));
			outputMessage("index "+index);
			destination = ( (Room)rooms.elementAt( index ) );
		}
		
		outputMessage("[CustomAI2 " + myName + "]: Target Destination: " + destination.getID() + ".\n");
		
		return destination; 
	}

	synchronized public Position goToRoom(String roomName) {
		Position destination = null;
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		if (!hasUsedGoToRoom){
			Vector rooms = gameController.getBoard().map.getNodes("rooms");
			
			for (int i = 0; i < rooms.size(); i++){
				if (((Room) rooms.elementAt(i)).getID().equals(roomName))
					destination = ((Room) rooms.elementAt(i));
			}
			outputMessage("[CustomAI2 " + myName + "]: Target Destination: " + destination.getID() + ".\n");
			
			return destination;
		}
		else
			return me.getPosition();
		 
	}	
		
	synchronized public boolean shouldAccuse() {
		boolean result = false;
		
		Card weapon = null;
		Card suspect = null;
		Card room = null;
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myCards = me.getCards();
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		int numberOfWeapons = 0;
		int numberOfRooms = 0;
		int numberOfSuspects = 0;
		
		for(int i = 0; i < myDCards.countCards(); i++) {
			if(myDCards.cardAt(i).isEliminated() == false) {
				if(myDCards.cardAt(i).getType().equals("Weapon"))
					numberOfWeapons++;
				else if(myDCards.cardAt(i).getType().equals("Suspect"))
					numberOfSuspects++;
				else if(myDCards.cardAt(i).getType().equals("Room"))
					numberOfRooms++;
			}
		}
		
		result = (numberOfWeapons == 1 && numberOfSuspects == 1 && numberOfRooms == 1);
	
		return result;
	}
	
	
	synchronized public boolean accuse(String suspect, String weapon, String room) {
		Card weapon_kuno = new Card(weapon, "Weapon");
		Card suspect_kuno = new Card(suspect, "Suspect");
		Card room_kuno = new Card(room, "Room");
		
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myCards = me.getCards();
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		outputMessage("[CustomAI2 " + myName + "]: I accuse " + suspect_kuno +
				" " + " using " + weapon_kuno + " at the " + room_kuno);
		
		return accuseCorrect(suspect_kuno, weapon_kuno, room_kuno);
	}
	
	synchronized public boolean accuseCorrect(Card suspectCard, Card weaponCard, Card roomCard) {
			boolean result = false;
			
			if(gameController.getBoard().getEnvelope().containsCard(weaponCard)  &&
					gameController.getBoard().getEnvelope().containsCard(roomCard) &&
					gameController.getBoard().getEnvelope().containsCard(suspectCard)
					)
			{
				result = true;
			}
			else {
				result = false;
			}
			
			return result;
	}
		
		
		
	public String suggest() {
		Card suspect = null;
		Card weapon = null;
		String output = "";
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		
		Vector possSuspects = new Vector();
		Vector possWeapons = new Vector();
		for(int i = 0; i < myDCards.countCards(); i++)
			if(myDCards.cardAt(i).getType().equals("Suspect") && myDCards.cardAt(i).isEliminated() == false)
				possSuspects.add(myDCards.cardAt(i));
		for(int i = 0; i < myDCards.countCards(); i++)
			if(myDCards.cardAt(i).getType().equals("Weapon") && myDCards.cardAt(i).isEliminated() == false)
				possWeapons.add(myDCards.cardAt(i));

		suspect = (Card)possSuspects.elementAt(  (int) (Math.random() * possSuspects.size())) ;
		weapon = (Card)possWeapons.elementAt( (int) (Math.random() * possWeapons.size())) ;
		
		Card room = gameController.getBoard().getCardPack().cardByName(me.getPosition().getID());
		
		
		Vector disproofInfo = findDisproveCandidate(suspect, weapon, room);
		
		Player playerCandidate = (Player)disproofInfo.firstElement();


		output += "- I suspect " + suspect.getName() + " of using the " + weapon.getName() + " in the " + me.getPosition().getID()+".\n";
		outputMessage(output);
		moveDefendant(suspect);
		
		
		if(playerCandidate != null) {
			disproofReceived = false;
			Message suggestionAlertMessage = new Message();
			Vector params = new Vector();
			params.add(new SuggestionAlert(suspect, weapon, room, me.getRealName()) );
			suggestionAlertMessage.parameters = params;
			suggestionAlertMessage.type = "suggestionAlert";
			suggestionAlertMessage.data = playerCandidate.getRealName();
			
			gameController.relayMessage(suggestionAlertMessage);
		}
		else {
			outputMessage("[CustomAI2 " + myName + "]: Nobody could disprove");
			
		}
		
		
		while ( disproofReceived == false ) {
			try {
				Thread.sleep(100);
			}
			catch(Exception e) {
				
			}
		}

		DisproofAlert disproofAlert = (DisproofAlert)incomingDisproof.parameters.firstElement();
		SuggestionAlert originalSuggestionAlert = disproofAlert.getOriginalSuggestion();
		
		String cardShown = disproofAlert.getCardShown();
		

		if(cardShown.equals("")) {
			if(me.getCards().containsCard(suspect) == false && 
					me.getCards().containsCard(weapon) == false &&
						me.getCards().containsCard(room) == false) {

				output+= "\nI secretly won because I know nobody else has the cards.\n";

				if( accuseCorrect(suspect, weapon, room) == true) {
					outputMessage("My accuse was true");
					Vector parameters = new Vector();
					parameters.add(me);
					gameController.directCommand("winGame", parameters );
				}
				else {
					outputMessage("[CustomAI2 " + myName + "]: My accuse was WRONG..!");
				}
			}
			else
				output += "[CustomAI2 " + myName + "]: Unable to disprove suggestion because I had the cards myself.";
		}
		else {
			outputMessage("[CustomAI2 " + myName + "]: Yeah, I made a good suggestion.");

		// We got a good response from the suggestion, one card has been eliminated.
			registerSuggestion(myDCards.cardByName(cardShown));
		}
		
		updateSuggestStatus();

		// Reset the waiting flag
		disproofReceived = false;
		return output;
	}
	
	public String suggest(String weapon, String suspect) {
		Card suspectCard = new Card(weapon, "Weapon");;
		Card weaponCard = new Card(suspect, "Suspect");;
		String output = "";
		Player me = gameController.getBoard().getPlayer(myName);
		CardCollection myDCards = me.getDetectivePad().getCardList();
		
		Card roomCard = gameController.getBoard().getCardPack().cardByName(me.getPosition().getID());
		
		if (!hasSuggested){
			Vector disproofInfo = findDisproveCandidate(suspectCard, weaponCard, roomCard);
			
			Player playerCandidate = (Player)disproofInfo.firstElement();
	
	
			output += "- I suspect " + suspectCard.getName() + " of using the " + weaponCard.getName() + " in the " + me.getPosition().getID()+".\n";
			outputMessage("[CustomAI2 " + myName + "]: " + output);
			moveDefendant(suspectCard);
			
			
			if(playerCandidate != null) {
				disproofReceived = false;
				Message suggestionAlertMessage = new Message();
				Vector params = new Vector();
				params.add(new SuggestionAlert(suspectCard, weaponCard, roomCard, me.getRealName()) );
				suggestionAlertMessage.parameters = params;
				suggestionAlertMessage.type = "suggestionAlert";
				suggestionAlertMessage.data = playerCandidate.getRealName();
				
				gameController.relayMessage(suggestionAlertMessage);
			}
			else {
				outputMessage("[CustomAI2 " + myName + "]: Nobody could disprove");
				
			}
			
			while ( disproofReceived == false ) {
				try {
					Thread.sleep(100);
				}
				catch(Exception e) {
					
				}
			}
			DisproofAlert disproofAlert = (DisproofAlert)incomingDisproof.parameters.firstElement();
			SuggestionAlert originalSuggestionAlert = disproofAlert.getOriginalSuggestion();
			
			String cardShown = disproofAlert.getCardShown();
	
	
			if(cardShown.equals("")) {
				if(me.getCards().containsCard(suspectCard) == false && 
						me.getCards().containsCard(weaponCard) == false &&
							me.getCards().containsCard(roomCard) == false) {
	
					output+= "\nI secretly won because I know nobody else has the cards.\n";
	
					if( accuseCorrect(suspectCard, weaponCard, roomCard) == true) {
						outputMessage("My accuse was true");
						Vector parameters = new Vector();
						parameters.add(me);
						gameController.directCommand("winGame", parameters );
					}
					else {
						outputMessage("[CustomAI2 " + myName + "]: My accuse was WRONG..!");
					}
				}
				else
					output += "Unable to disprove suggestion because I had the cards myself.";
			}
			else {
				outputMessage("[CustomAI2 " + myName + "]: Yeah, I made a good suggestion.");
	
			// We got a good response from the suggestion, one card has been eliminated.
				registerSuggestion(myDCards.cardByName(cardShown));
			}
			
			updateSuggestStatus();
	
			// Reset the waiting flag
			disproofReceived = false;
		}
		else {
			output += "I have suggested already!";
		}
		return output;
	} //end suggest 
	
	public void updateSuggestStatus() {
		Vector param = new Vector();
		param = new Vector();
		param.add(gameController.getBoard().getPlayer(myName));
		gameController.directCommand("markSuggestMade", param);
	}

	public void registerSuggestion(String shown) {
		Vector param = new Vector();
		param.add(shown);
		gameController.directCommand("eliminateCard", param);
	}

	private void registerSuggestion(Card shown) {
		registerSuggestion(shown.getName());
	}
				
				
	synchronized public void movePlayer(Position target, Vector path, int amount) {
		String output = "";
		Vector pathTaken = applyConstraint(target, path);

		Player me = gameController.getBoard().getPlayer(myName);
		output = "#movePlayer Moving to " + target.getID();
		me.setMovesRemaining(0);
		Vector param = new Vector();
		param.add(target);
		param.add(me);
		param.add(pathTaken);
		gameController.directCommand("movePlayer", param);
		
		outputMessage ("[CustomAI2 " + myName + "]: " + output );

	}
	
	private Vector applyConstraint(Position target, Vector path) {
		Vector altPath = new Vector();
		
		for(int i=0;i<path.size();i++) {
			if(path.elementAt(i) == target) {
				altPath.add(path.elementAt(i));
				break;
			}
			else
				altPath.add(path.elementAt(i));
		}

		return altPath;
	}


	public int rollDice() {
		Player me = gameController.getBoard().getPlayer(myName);
		
		if (!hasRolledDice){
			gameController.directCommand("roll", new Vector());
			hasRolledDice = true;
			return me.getMovesRemaining();
		}
		else
			return 0;
		

	}
	
	public Vector findDisproveCandidate( Card who, Card what, Card where ) {
		Vector results = new Vector();
		
		String output = "";
		Player playerWithCard = null;
		Vector cardsFound = new Vector();
		
		Player currentPlayer = gameController.getBoard().getCurrentPlayer();

		
		Player defendant = gameController.getBoard().getCharacter(who.getName());
		
		if(!currentPlayer.getCharacter().equals(who.getName())) {
			if(defendant != null) {
				Position oldRoom = defendant.getPosition();
				Position destination = currentPlayer.getPosition();

				Vector parameters = new Vector();
				parameters.add(destination);
				parameters.add(defendant);		// Who to move..
				gameController.directCommand( "movePlayer", parameters );
			}
		}
                
		playerWithCard = null;
		cardsFound = new Vector();
		
		Vector playersToParse = localArrangePlayersBy(currentPlayer);

		for(int i = 0; i < playersToParse.size(); i++) {
			playerWithCard = (Player)playersToParse.elementAt(i);
			if(playerWithCard != currentPlayer) {
				if(playerWithCard.hasCard(who))
					cardsFound.add(who);
				if(playerWithCard.hasCard(what))
					cardsFound.add(what);
				if(playerWithCard.hasCard(where))
					cardsFound.add(where);
				if(cardsFound.size() > 0)
					break;
			}
		}
		
		if(cardsFound.size() > 0) {
			output += playerWithCard.getRealName() + " choose which card you will use to disprove :-\n ";
			for(int i = 0; i < cardsFound.size(); i++) {
				output += ((Card)cardsFound.elementAt(i)).getName() + " ";
			}
			//gameConfig.setDisproveMode(true);
		}
		else {
			output += "Unable to disprove the suggestion.";
		}			
		
		outputMessage("[CustomAI2 " + myName + "]: "+output);
		
		results.add(playerWithCard);
		results.add(cardsFound);
		
		return results;
	}
		




	private Vector localArrangePlayersBy(Player currentPlayer) {
		Vector players = new Vector();
		int playerPosition = gameController.getBoard().getPlayerIndex(currentPlayer);
		Vector livePlayers = gameController.getBoard().getPlayers();
		
		for(int i = playerPosition + 1; i < livePlayers.size(); i++)
			players.add(livePlayers.elementAt(i));
		if(!( playerPosition == 0)) 
				for(int i = 0; i < playerPosition; i++)
					players.add(livePlayers.elementAt(i));
		return players;

	}
  
	public void moveDefendant(Card suspect) {
		Player defendant = gameController.getBoard().getCharacter(suspect.getName());
		Player me = gameController.getBoard().getPlayer(myName);

		Card testCard = null;
		Card room = gameController.getBoard().getCardPack().cardByName(me.getPosition().getID());
		if(defendant != null) {
			String output = "";
	        	
			if(defendant != null) {
				Position oldRoom = defendant.getPosition();
				Position destination = me.getPosition();
				
				Vector parameters = new Vector();
				parameters.add(destination);
				parameters.add(defendant);
				gameController.directCommand( "movePlayer", parameters );
			}
                	
        		output = "Called " + defendant.getRealName() + " into the " + room.getName();
        	
			outputMessage(output);
		}
	}


	public void movePlayer(Position target, int amount) {
		
	}
	
	private void runDecideOnMovementEvent(){
		if (i == 0){
			if (accuse("MissScarlet", "Rope", "Kitchen")==true){
				gameController.directCommand("winGame", null);
			}
			else {
				gameController.directCommand("removeInteractive", null);
				hasMadeFalseAccusation=true;
			}
		}
		sleepFor(sleepPeriod);
		
	}
	
	private void runSuggestOrAccuseEvent(){
		Player me = gameController.getBoard().getPlayer(myName);
		if (me.canSuggestHere() == true && me.canSuggest() == true){
					if (i == 1){
						suggest("Rope", "MissScarlet");
					}
					else if (i/2 == 2){
						suggest("Spanner", "ProfessorPlum");
					}
					else if (i/2 == 3){
						suggest("Spanner", "MissScarlet");
					}
					else if (i/2 == 4){
						suggest("Revolver", "MissScarlet");
					}
					else if (i/2 == 5){
						suggest("Dagger", "MissScarlet");
					}
					else if (i/2 == 6){
						suggest("LeadPipe", "MissScarlet");
					}
					else if (i/2 == 7){
						suggest("CandleStick", "MissScarlet");
					}
					else if (i/2 == 8){
						suggest("Rope", "MissScarlet");
					}
					else if (i/2 == 9){
						suggest("Spanner", "MissScarlet");
					}
					else if (i/2 == 10){
						suggest("Dagger", "ProfessorPlum");
					}
					else
						i = 20;
		}
		else {
			if (i == 1) goToRoom("Kitchen");
			else if (i == 2) goToRoom("Hall");
			else if (i == 3) goToRoom("Conservatory");
			else if (i == 4) goToRoom("Study");
			else if (i == 5) goToRoom("BilliardRoom");
			else if (i == 6) goToRoom("Library");
			else if (i == 7) goToRoom("Ballroom");
			else if (i == 8) goToRoom("Lounge");
			else if (i == 9) goToRoom("DiningRoom");
			else
				i = 10;
		}
				
				
				i--;
				endGo();
	}
	
	private void handleSuggestionAlert(Message message) {

		outputMessage("\n[CustomAI2 " + myName + "]: Heard a suggestionAlert directed at me...\n");
		Message disproofAlert = new Message();
		String chosenCard = "";
		
		disproofAlert.type = "disproofAlert";
		
		SuggestionAlert alert = (SuggestionAlert)message.parameters.elementAt(0);
		Player me = gameController.getBoard().getPlayer(myName);
		
		
		if (!hasDisproved){
			
			
		}
		
		disproofAlert.parameters.add(new DisproofAlert( chosenCard, alert, myName ));

		gameController.relayMessage(disproofAlert);
		
		outputMessage("\n[CustomAI2 " + myName + "]: Relay of disproofAlert... Ok\n");

	}
	
	private void deductFromDisproof(DisproofAlert d) {
		suggester = d.getDisprovedBy();
		disprover = d.getOriginalSuggestion().suggestedBy;
		if (!hasListened){
			hasListened=true;
			
			if (!disprover.equals("")) disproved = true;
			else disproved = false;
			
		}

		
		
		
		
		
	}


	@Override
	public boolean makeAccusation() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void writeLogToFile(){
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("log1.txt"));
			out.write(screenOutput);
			out.newLine();
			out.close();
		}
		catch (IOException e)
		{
			System.out.println("Exception ");		
		}
	}

 }
